package com.meshnetics.gb.type.impl.zcl;

import com.meshnetics.gb.type.impl.ByteUtils;

import java.nio.ByteBuffer;

/**
 * WRITE DESCRIPTION HERE
 *
 * @author AKovylin
 */
public class Bool extends AbstractDataValue implements ZCLInvalidable {
    protected final byte value;

    protected Bool(TypeBool type, byte value) {
        super(type);
        this.value = value;
    }

    public boolean isInvalid() {
        return value == TypeBool.INVALID;
    }

    public int getDataSize() {
        return 1;
    }

    public Object toObject() {
        Object res;
        switch (value) {
            case TypeBool.FALSE:
                res = Boolean.FALSE;
                break;
            case TypeBool.TRUE:
                res = Boolean.TRUE;
                break;
            case TypeBool.INVALID:
                res = null;
                break;
            default:
                throw new IllegalStateException("not a boolean");
        }
        return res;
    }

    public byte[] bytesValue() {
        return new byte[]{value};
    }

    public void serialize(ByteBuffer bb) {
        ByteUtils.ensureNotNull(bb);
        bb.put(value);
    }

    public boolean equals(Object obj) {
        return super.equals(obj) && obj instanceof Bool &&
                this.value == ((Bool) obj).value;
    }

    public int hashCode() {
        return super.hashCode() + 29 * value;
    }
}
